package com.winsdom.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
    //appid
    @Value("${alipay.app-id}")
    private String APP_ID;

    //应用私钥
    @Value("${alipay.app-private-key}")
    private String APP_PRIVATE_KEY;

    @Value("${alipay.charset}")
    private String CHARSET;

    // 支付宝公钥
    @Value("${alipay.alipay-public-key}")
    private String ALIPAY_PUBLIC_KEY;

    //这是沙箱接口路径
    @Value("${alipay.gateway-url}")
    private String GATEWAY_URL;

    @Value("${alipay.format}")
    private String FORMAT;

    //签名方式
    @Value("${alipay.sign-type}")
    private String SIGN_TYPE;

    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    @Value("${alipay.notify-url}")
    private String NOTIFY_URL;// POST 异步通知

    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    @Value("${alipay.return-url}")
    private String RETURN_URL;// GET 同步跳转

    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", outTradeNo);
        bizContent.put("total_amount", String.format("%.2f", totalAmount));
        bizContent.put("subject", subject);
        bizContent.put("body", "");
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        bizContent.put("timeout_express", "30m");

        alipayRequest.setBizContent(bizContent.toJSONString());

        // 打印用于调试：确认 subject 是原始中文
        System.out.println("生成的 biz_content: " + bizContent);

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());

        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
